package com.sinoglobal.ningxia.adapter;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.orhanobut.logger.Logger;
import com.sinoglobal.ningxia.R;
import com.sinoglobal.ningxia.beans.LiveHomeBean;
import com.sinoglobal.ningxia.http.ConnectionUtil;
import com.sinoglobal.ningxia.utils.TimeUtil;

import net.tsz.afinal.FinalBitmap;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by XINDZ on 2017/2/15.
 * 功能:直播预告的Adapter
 */

public class LiveForeShowAdapter extends BaseAdapter {
    private LayoutInflater mInflater;
    List<LiveHomeBean.List1Bean> foreShowtList = new ArrayList();
    Context context;
    FinalBitmap fb;

    public void setForeShowtList(List<LiveHomeBean.List1Bean> foreShowtList) {
        this.foreShowtList = foreShowtList;
        notifyDataSetChanged();
    }

    public LiveForeShowAdapter(Context context) {
        this.context = context;
        mInflater = LayoutInflater.from(context);
        fb = FinalBitmap.create(context);
    }

    public LiveForeShowAdapter(Context context, List<LiveHomeBean.List1Bean> foreShowtList) {
        this.context = context;
        this.foreShowtList = foreShowtList;
        mInflater = LayoutInflater.from(context);
        fb = FinalBitmap.create(context);
    }

    @Override
    public int getCount() {
        return foreShowtList.size();
    }

    @Override
    public Object getItem(int i) {
        return foreShowtList.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        final Holder holder;
        long contDownTime = 0;
        if (view == null) {
            holder = new Holder();
            view = mInflater.inflate(R.layout.item_foreshow, null);
            holder.iv_foreshow_logo = (ImageView) view.findViewById(R.id.iv_foreshow_logo);
            holder.tv_foreshow_title = (TextView) view.findViewById(R.id.tv_foreshow_title);
            holder.tv_foreshow_intro = (TextView) view.findViewById(R.id.tv_foreshow_intro);
            holder.tv_foreshow_date = (TextView) view.findViewById(R.id.tv_foreshow_date);
            view.setTag(holder);
        } else {
            holder = (Holder) view.getTag();
        }
        LiveHomeBean.List1Bean list1Bean = foreShowtList.get(i);
        String title = list1Bean.getTitle();
        String img = list1Bean.getImg();
        String content = list1Bean.getContent();
        long stime = list1Bean.getStime();
        holder.tv_foreshow_title.setText(title);//标题
        holder.tv_foreshow_intro.setText(content);//简介
        Bitmap bitmap = BitmapFactory.decodeResource(context.getResources(), R.drawable.default43);
        //头像
        if (img != null && !img.equals("")) {
            fb.display(holder.iv_foreshow_logo, ConnectionUtil.localUrl + img, bitmap, bitmap, true);
        } else {
            holder.iv_foreshow_logo.setBackgroundResource(R.drawable.default43);
        }

        long lEnd = stime * 1000;

        String date = TimeUtil.getLongPointDate(lEnd);
        String[] split = date.split(" ");
        String showTime = split[0] + "\n" + split[1];
        Logger.i("切割后时间:" + showTime + "切割前时间:" + date);
        holder.tv_foreshow_date.setText(showTime);//设置时间

        return view;
    }


    static class Holder {
        public ImageView iv_foreshow_logo;
        public TextView tv_foreshow_title, tv_foreshow_intro, tv_foreshow_date, tv_foreshow_downtime;

    }
}
